package com.ruoyi.ade.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ade.domain.Standard;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ade.domain.Emr;
import com.ruoyi.ade.service.IEmrService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * ADE DatasetController
 * 
 * @author wxh
 * @date 2025-06-05
 */
@RestController
@RequestMapping("/ade/emr")
public class EmrController extends BaseController
{
    @Autowired
    private IEmrService emrService;

    @PutMapping("/format")
    public AjaxResult format(){
        boolean r = emrService.fromat();
        if(r) return AjaxResult.success();
        return AjaxResult.error("Formatting is goning on..");
    }

    /**
     * 查询ADE Dataset列表
     */
    @PreAuthorize("@ss.hasPermi('ade:emr:list')")
    @GetMapping("/list")
    public TableDataInfo list(Emr emr)
    {
        startPage();
        List<Emr> list = emrService.selectEmrList(emr);
        return getDataTable(list);
    }

    /**
     * 导出ADE Dataset列表
     */
    @PreAuthorize("@ss.hasPermi('ade:emr:export')")
    @Log(title = "ADE Dataset", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Emr emr)
    {
        List<Emr> list = emrService.selectEmrList(emr);
        ExcelUtil<Emr> util = new ExcelUtil<Emr>(Emr.class);
        util.exportExcel(response, list, "ADE Dataset数据");
    }

    /**
     * 获取ADE Dataset详细信息
     */
    @PreAuthorize("@ss.hasPermi('ade:emr:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(emrService.selectEmrById(id));
    }

    /**
     * 随机获取一条满足条件的数据
     * @param emr
     * @return
     */
    @PostMapping(value = "/random")
    public AjaxResult random(@RequestBody Emr emr){
        return AjaxResult.success(emrService.selectOneRandomEmr(emr));
    }

    /**
     * 新增ADE Dataset
     */
    @PreAuthorize("@ss.hasPermi('ade:emr:add')")
    @Log(title = "ADE Dataset", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Emr emr)
    {
        emr.setCreateBy(getUsername());
        return toAjax(emrService.insertEmr(emr));
    }

    /**
     * 修改ADE Dataset
     */
    @PreAuthorize("@ss.hasPermi('ade:emr:edit')")
    @Log(title = "ADE Dataset", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Emr emr)
    {
        emr.setUpdateBy(getUsername());
        return toAjax(emrService.updateEmr(emr));
    }

    /**
     * 删除ADE Dataset
     */
    @PreAuthorize("@ss.hasPermi('ade:emr:remove')")
    @Log(title = "ADE Dataset", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(emrService.deleteEmrByIds(ids));
    }
}
